package com.sunway.paisong.pos.service;

import java.util.List;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class CheckService extends Service {

	public static final String ACTION = "com.sunway.paisong.pos.service.CheckService";

	private String cheakServiceName = "com.sunway.paisong.pos.service.PosReportService";

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i("sw", "CheckService.onCreate");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (!isReportServiceRunning(cheakServiceName)) {
			Log.i("sw", "startService");
			Intent it = new Intent(this, PosReportService.class);
			this.startService(it);
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		Log.i("sw", "CheckService.onDestroy");
		super.onDestroy();
	}

	private boolean isReportServiceRunning(String reportServiceClassName) {
		ActivityManager mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

		List<ActivityManager.RunningServiceInfo> mServiceList = mActivityManager
				.getRunningServices(50);

		for (int i = 0; i < mServiceList.size(); i++) {
			if (reportServiceClassName.equals(mServiceList.get(i).service.getClassName())) {
				Log.i("sw", "isRunning");
				return true;
			}
		}
		return false;
	}

}
